package com.newtouch.bxzs.database.controller;

import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.database.service.CheckVerCodeService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 基础公共接口Controller类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2020年12月29日</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author Amour
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = "/baseCommon")
public class BaseCommonController {

	/** 静态变量：系统日志 */
	private static final Log logger = LogFactory.getLog(BaseCommonController.class);

	@Autowired
	CheckVerCodeService checkVerCodeService;

	@Autowired
	RedisUtils redisUtils;
	// @Autowired
	// SimulationClient simulationClient;

	private Map<String, String> verCodeMap = new HashMap<String, String>();

	/**
	 * 获取验证码接口，统一走该接口
	 * 
	 * @param operationId
	 *            操作ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@GetMapping(value = "/getVerCode")
	public BaseResponse<String> getVerCode(@RequestParam("operationId") String operationId) {
		logger.info("获取验证码");
		StringBuilder str = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			str.append(random.nextInt(10));
		}
		String verCode = str.toString();
		// String verCode = simulationClient.getVerCode();
		// TODO 暂时使用map进行保存，后续修改为放入REDIS
		verCodeMap.put(operationId, verCode);
		return RespGenerator.returnOK(verCode);
	}

	/**
	 * 校验验证码，统一走该接口
	 * 
	 * @param operationId
	 *            操作ID
	 * @param verCode
	 *            验证码
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@GetMapping(value = "/checkVerCode")
	public BaseResponse<Boolean> checkVerCode(@RequestParam("operationId") String operationId,
			@RequestParam("verCode") String verCode) {
		logger.info("校验验证码");
		return RespGenerator.returnOK(checkVerCodeService.checkVerCode(verCodeMap, operationId, verCode));
	}

	/**
	 *立即注册和忘记密码开关
	 *
	 * @return String 开关 1-开
	 */
	@SuppressWarnings("unchecked")
	@GetMapping(value = "/getSystemParameter")
	public BaseResponse<String> getSystemParameter() {
		logger.info("立即注册和忘记密码开关");
		return RespGenerator.returnOK(redisUtils.get(SystemParamsEnum.REGISTER_AND_FORGET_PASSWORD_ON_OFF.getCode()));
	}
}
